{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "positions = get_position() # get_position - 查询指定交易账户的全部持仓信息\n",
    "position_long = [p for p in positions if p['symbol'] == symbol and p['side'] == PositionSide_Long]\n",
    "列表推导式（list comprehension），\n",
    "目的是从一个名为 positions 的列表中筛选出符合特定条件的字典项，并将这些符合条件的字典项存储在一个新的列表 position_long 中。\n",
    "p 是列表推导式中的临时变量，代表 positions 列表中的每一个字典项。\n",
    "从 positions 列表中筛选出所有 'symbol' 键对应的值等于 symbol 变量值，并且 'side' 键对应的值等于 PositionSide_Long 的持仓信息，\n",
    "并将这些符合条件的持仓信息存储在 position_long 列表中。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'symbol': 'AAPL', 'side': 'Long', 'quantity': 100}, {'symbol': 'AAPL', 'side': 'Long', 'quantity': 200}]\n"
     ]
    }
   ],
   "source": [
    "# 假设 positions 列表和相关变量已经定义\n",
    "positions = [\n",
    "    {'symbol': 'AAPL', 'side': 'Long', 'quantity': 100},\n",
    "    {'symbol': 'GOOGL', 'side': 'Short', 'quantity': 50},\n",
    "    {'symbol': 'AAPL', 'side': 'Long', 'quantity': 200}\n",
    "]\n",
    "symbol = 'AAPL'\n",
    "PositionSide_Long = 'Long'\n",
    "\n",
    "# 使用列表推导式筛选符合条件的持仓信息\n",
    "position_long = [p for p in positions if p['symbol'] == symbol and p['side'] == PositionSide_Long]\n",
    "\n",
    "# 输出结果\n",
    "print(position_long)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
